import { Character, AbilityType, GameContext } from '../types';

// 商业大亨 - Business Tycoon
const businessTycoonPassive = {
  id: 'business-tycoon-passive',
  name: '商业嗅觉',
  description: '购买地块时获得10%折扣',
  type: AbilityType.PASSIVE,
  effect: (context: GameContext) => {
    // Applied in PropertyManager.buyProperty
  },
};

const businessTycoonActive = {
  id: 'business-tycoon-active',
  name: '强制交换',
  description: '强制与任意玩家交换一个地块',
  type: AbilityType.ACTIVE,
  cooldownTurns: 5,
  effect: (context: GameContext) => {
    // Implemented in CharacterManager.useActiveAbility
  },
};

export const businessTycoon: Character = {
  id: 'business-tycoon',
  name: '商业大亨',
  description: '精明的商人，擅长以更低的价格获取资产',
  avatar: '💼',
  passiveAbility: businessTycoonPassive,
  activeAbility: businessTycoonActive,
};

// 幸运女神 - Lucky Charm
const luckyCharmPassive = {
  id: 'lucky-charm-passive',
  name: '幸运之星',
  description: '掷骰子时可以重投一次（每回合一次）',
  type: AbilityType.PASSIVE,
  effect: (context: GameContext) => {
    // Applied in GameManager.rollDice
    context.currentPlayer.canReroll = true;
  },
};

const luckyCharmActive = {
  id: 'lucky-charm-active',
  name: '幸运护盾',
  description: '下一次停留的位置免除所有费用',
  type: AbilityType.ACTIVE,
  cooldownTurns: 4,
  effect: (context: GameContext) => {
    // Implemented in CharacterManager.useActiveAbility
    // Sets a flag that's checked in handleSpaceAction
  },
};

export const luckyCharm: Character = {
  id: 'lucky-charm',
  name: '幸运女神',
  description: '运气爆棚，总能在关键时刻转危为安',
  avatar: '🍀',
  passiveAbility: luckyCharmPassive,
  activeAbility: luckyCharmActive,
};

// 建筑师 - Architect
const architectPassive = {
  id: 'architect-passive',
  name: '建筑大师',
  description: '升级建筑物成本降低20%',
  type: AbilityType.PASSIVE,
  effect: (context: GameContext) => {
    // Applied in PropertyManager.upgradeProperty
  },
};

const architectActive = {
  id: 'architect-active',
  name: '免费升级',
  description: '立即升级一个地块而不支付费用',
  type: AbilityType.ACTIVE,
  cooldownTurns: 6,
  effect: (context: GameContext) => {
    // Implemented in CharacterManager.useActiveAbility
  },
};

export const architect: Character = {
  id: 'architect',
  name: '建筑师',
  description: '建筑专家，能以更低成本打造资产帝国',
  avatar: '🏗️',
  passiveAbility: architectPassive,
  activeAbility: architectActive,
};

// 银行家 - Banker
const bankerPassive = {
  id: 'banker-passive',
  name: '银行关系',
  description: '每次经过起点额外获得50%奖励',
  type: AbilityType.PASSIVE,
  effect: (context: GameContext) => {
    // Applied in GameManager.movePlayer when passing GO
  },
};

const bankerActive = {
  id: 'banker-active',
  name: '无息贷款',
  description: '从银行获得初始资金50%的无息贷款',
  type: AbilityType.ACTIVE,
  cooldownTurns: 5,
  effect: (context: GameContext) => {
    const loanAmount = Math.floor(context.gameState.config.initialMoney * 0.5);
    context.currentPlayer.money += loanAmount;
  },
};

export const banker: Character = {
  id: 'banker',
  name: '银行家',
  description: '金融专家，拥有强大的资金运作能力',
  avatar: '🏦',
  passiveAbility: bankerPassive,
  activeAbility: bankerActive,
};

// 旅行者 - Traveler
const travelerPassive = {
  id: 'traveler-passive',
  name: '自由移动',
  description: '可以选择前进或后退',
  type: AbilityType.PASSIVE,
  effect: (context: GameContext) => {
    // Applied in GameManager.movePlayer
    // Player can choose direction
  },
};

const travelerActive = {
  id: 'traveler-active',
  name: '瞬间移动',
  description: '传送到棋盘上任意位置',
  type: AbilityType.ACTIVE,
  cooldownTurns: 7,
  effect: (context: GameContext) => {
    // Implemented in CharacterManager.useActiveAbility
    // Allows player to choose any position
  },
};

export const traveler: Character = {
  id: 'traveler',
  name: '旅行者',
  description: '行动自由，可以灵活选择移动方向',
  avatar: '🧳',
  passiveAbility: travelerPassive,
  activeAbility: travelerActive,
};

// 谈判专家 - Negotiator
const negotiatorPassive = {
  id: 'negotiator-passive',
  name: '谈判技巧',
  description: '支付给其他玩家的租金减少15%',
  type: AbilityType.PASSIVE,
  effect: (context: GameContext) => {
    // Applied in PropertyManager.calculateRent
  },
};

const negotiatorActive = {
  id: 'negotiator-active',
  name: '强制谈判',
  description: '强制发起一次交易，对方必须回应',
  type: AbilityType.ACTIVE,
  cooldownTurns: 4,
  effect: (context: GameContext) => {
    // Implemented in CharacterManager.useActiveAbility
    // Creates a trade that must be responded to
  },
};

export const negotiator: Character = {
  id: 'negotiator',
  name: '谈判专家',
  description: '善于交涉，能够降低支出并促成交易',
  avatar: '🤝',
  passiveAbility: negotiatorPassive,
  activeAbility: negotiatorActive,
};

// 导出所有角色
export const allCharacters: Character[] = [
  businessTycoon,
  luckyCharm,
  architect,
  banker,
  traveler,
  negotiator,
];

// 根据ID获取角色
export function getCharacterById(id: string): Character | undefined {
  return allCharacters.find(char => char.id === id);
}

// 获取随机角色
export function getRandomCharacter(): Character {
  return allCharacters[Math.floor(Math.random() * allCharacters.length)];
}
